package cn.iocoder.yudao.module.scrm.service.marketing.transmit;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.scrm.controller.admin.marketing.vo.transmit.MarketingTransmitPageReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.marketing.vo.transmit.MarketingTransmitSaveReqVO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.marketing.MarketingTransmitDO;
import cn.iocoder.yudao.module.scrm.dal.mysql.marketing.MarketingTransmitMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.scrm.enums.ErrorCodeConstants.*;

/**
 * 营销转发 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class MarketingTransmitServiceImpl implements MarketingTransmitService {

    @Resource
    private MarketingTransmitMapper marketingTransmitMapper;

    @Override
    public Integer createMarketingTransmit(MarketingTransmitSaveReqVO createReqVO) {
        // 插入
        MarketingTransmitDO marketingTransmit = BeanUtils.toBean(createReqVO, MarketingTransmitDO.class);
        marketingTransmitMapper.insert(marketingTransmit);
        // 返回
        return marketingTransmit.getId();
    }

    @Override
    public void updateMarketingTransmit(MarketingTransmitSaveReqVO updateReqVO) {
        // 校验存在
        validateMarketingTransmitExists(updateReqVO.getId());
        // 更新
        MarketingTransmitDO updateObj = BeanUtils.toBean(updateReqVO, MarketingTransmitDO.class);
        marketingTransmitMapper.updateById(updateObj);
    }

    @Override
    public void deleteMarketingTransmit(Integer id) {
        // 校验存在
        validateMarketingTransmitExists(id);
        // 删除
        marketingTransmitMapper.deleteById(id);
    }

    private void validateMarketingTransmitExists(Integer id) {
        if (marketingTransmitMapper.selectById(id) == null) {
            throw exception(MARKETING_TRANSMIT_NOT_EXISTS);
        }
    }

    @Override
    public MarketingTransmitDO getMarketingTransmit(Integer id) {
        return marketingTransmitMapper.selectById(id);
    }

    @Override
    public PageResult<MarketingTransmitDO> getMarketingTransmitPage(MarketingTransmitPageReqVO pageReqVO) {
        return marketingTransmitMapper.selectPage(pageReqVO);
    }

}